(function () {
    'use strict';

    angular.module('app').controller('wct.user', WctUserCtrl);

    WctUserCtrl.$inject = [
        '$scope',
        '$filter',
        '$win',
        '$toast',
        'A',
        'Api',
        'WctUserService'
    ];
    function WctUserCtrl($scope, $filter, $win, $toast, A, Api, WctUserService) {
        console.log($toast);

        /**
         * map: key为openId, value为nickname的对象
         */
        var showSendDialog = function (map) {
            var dialog = $win.open({
                template    : 'sendMsgDialog',
                controllerAs: 'sendCtrl',
                controller  : function () {
                    var vm = this;
                    vm.content = "";
                    vm.send = function () {
                        if (vm.content === '') {
                            return $toast.info('请输入发送内容!');
                        }
                        WctUserService.send(vm.content, Object.keys(map).join(',')).then(function (result) {
                            if (result.success) {
                                $toast.success('消息发送成功!');
                            } else {
                                var failUsers = [];
                                angular.forEach(result.data, function (value, key) {
                                    failUsers.push(map[key]);
                                });
                                $toast.error('用户 ' + failUsers.join(', ') + ' 消息发送失败!');
                            }
                            dialog.close();
                        });
                    };
                }
            });
        };

        // ----------------------------------------
        // table
        $scope.options = {
            api     : Api.wct.user.page,
            checkbox: true,
            column  : [{
                text : '序号',
                index: true
            }, {
                text : '昵称',
                name : 'nickname',
                ratio: 3 //列宽比列, 不配置时默认1
            }, {
                text: '性别',
                name: 'sex'
            }, {
                text    : '地址',
                name    : 'address',
                ratio   : 2,
                renderer: function (value) {
                    var country  = value.country,
                        province = value.province,
                        city     = value.city;
                    var address = [];
                    country && address.push(country);
                    province && address.push(province);
                    city && address.push(city);
                    return address.join("-");
                }
            }, {
                text    : '关注时间',
                name    : 'subscribeTime',
                renderer: function (row) {
                    return $filter('date')(row.subscribeTime * 1000);
                }
            }, {
                text : '操作列',
                name : 'action',
                items: [{
                    icon   : 'fa fa-comments',
                    tooltip: '发送消息',
                    handler: function (o, row) {
                        var map = {};
                        map[row.openId] = row.nickname;
                        showSendDialog(map);
                    }
                }, {
                    icon   : 'fa fa-tasks',
                    tooltip: '详情',
                    handler: function (o, row) {
                        $win.open({
                            template: 'detailDialog',
                            data    : row
                        });
                    }
                }]
            }]
        };

        // ----------------------------------------
        // toolbar

        // 初始化 table查询参数(根据 ag-search-params 参数配置的)
        $scope.params = {};
        $scope.progress = A.PromiseTracker();

        $scope.search = function (name) {
            $scope.params.userName = name;
        };

        // 发送消息
        $scope.send = function () {
            var sels = $scope.userTable.getSelectRow();
            if (!sels.length) {
                return $toast.info('请选择用户');
            }
            var map = {};
            angular.forEach(sels, function (item, i) {
                map[item.openId] = item.nickname;
            });
            showSendDialog(map);
        };
        // 同步数据
        $scope.sync = function () {
            var promise = WctUserService
                .sync()
                .then(function (result) {
                    result.success && $toast.info("同步成功!");
                    !result.success && $toast.error(result.msg || "同步失败!");
                    $scope.userTable.reload();
                });
            $scope.progress.addPromise(promise);
        };
    }
})
();